<template>
    <div class="">
        <el-dialog :title="isAdd ? '添加商品规格' : '编辑商品规格'" :visible.sync="isShow" :before-close="cancel" center>
            <el-form :model="specslist" ref="specslist" :rules="rules">
                <el-form-item label="商品规格名称" :label-width="formLabelWidth" prop="specsname">
                    <el-input autocomplete="off" v-model="specslist.specsname"></el-input>
                </el-form-item>
                <el-form-item label="规格属性" :label-width="formLabelWidth" v-for="(item, index) in attrArr">
                    <el-input class="inp_Info" v-model="item.value" autocomplete="off"></el-input>
                    <el-button v-if="index == 0" @click="add_info" type="primary">新增规格属性</el-button>
                    <el-button @click="del_info(index)" v-else type="danger">删除</el-button>

                </el-form-item>

                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="specslist.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { specsAdd, specsEdit, getspecsInfo } from '../../request/api';
import { mapActions } from 'vuex';
export default {
    props: ['isShow', 'isAdd'],
    data() {
        return {
            specslist: {
                specsname: "", //商品规格名称
                attrs: "", //规格属性值"白色,红色，黄色"
                status: 1, //状态1正常2禁用
            },
            rules: {
                specsname: [
                    { required: true, message: "请输入商品规格名称", trigger: "blur" },
                    {
                        min: 2,
                        max: 15,
                        message: "长度在 2 到 15 个字符",
                        trigger: "blur",
                    },
                ],
            },
            formLabelWidth: '120px',
            attrArr: [
                {
                    value: ''
                },
            ],
        }
    },
    methods: {
        cancel() {
            this.$emit('cancel', false)
            this.reset();
        },
        add_info() {
            if (this.attrArr.length > 4) {
                this.$message.warning("最多添加4项,不能再加了。");
            } else {
                this.attrArr.push({
                    value: "",
                });
            }
        },
        lookup(id) {
            getspecsInfo({ id }).then((res) => {
                if (res.code == 200) {
                    this.specslist = res.list[0];
                    this.specslist.id = id;
                    
                    this.specslist.attrs.map((item, index) => {
                        // console.log(item, "iiiii");
                        if (index == 0) {
                            this.attrArr[0].value = item;
                        } else {
                            this.attrArr.push({
                                value: item,
                            });
                        }
                    });
                }
            });
        },
        //封装一个清空事件
        reset() {
            this.specslist = {
                specsname: "", //商品规格名称
                attrs: "", //规格属性值
                status: 1, //状态1正常2禁用
            };
            //调用清空验证的方法
            this.$refs.specslist.resetFields();
            //清空动态表单项
            this.attrArr = [
                {
                    value: "",
                },
            ];
        },
        del_info(n) {
            this.attrArr.splice(n, 1)
        },
        ...mapActions({
            getSpecs: 'specs/getSpecsListAction',
            getCount: 'specs/getCountAction'
        }),
        confirm() {
            this.$refs.specslist.validate((valid) => {
                if (valid) {
                    this.specslist.attrs = this.attrArr.map((item) => item.value).join(',')
                    if (this.isAdd) {
                        specsAdd(this.specslist).then((res) => {
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel();
                                this.getSpecs();
                                this.getCount()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        specsEdit(this.specslist).then((res) => {
                            console.log(res, '////');
                            if (res.code == 200) {
                                this.$message.success(res.msg)
                                this.cancel();
                                this.getSpecs();
                                this.getCount()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                } else {
                    this.$message.error('验证失败')
                    return false
                }
            })
        }
    }

}
</script>
<style scoped>
.inp_Info {
    width: 62%;
}
</style>